<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class config extends CI_Controller {

    var $theme_data = array(
        'layout_main' => 'layout',
        'left' => 'include/leftcp',
    );
	
    function __construct() {
        parent::__construct();
        $this->load->model("muser");
        $this->load->model("mconfig");
        $this->load->model("mresource");
		
		$this->resCode = $this->mconfig->resCode;
        $this->url_index = admin_url() . "config";
        $this->folder_upload = "dataupload/config/";
        $this->url_edit = $this->url_index . "/edit/";
        $this->url_delete = $this->url_index . "/delete/";
        $this->url_add = $this->url_index . "/edit/0";
    }

    public function index() {
        $data = array();
        $data['conf'] = $this->mconfig->gets();
        $data['tresource'] = $this->mresource->get($this->resCode,'RES_CODE');
		//$kq = $this->mconfig->update('EMAIL_ADMINxdddd','CONF_CODE', array('CONF_VALUE'=>1));
		$post = $this->input->post();
		print_r( $data['conf']);
		if ($post) {
			print_r($post);
		}
        //$this->theme_data["main"] = "config/index";
		$this->theme_data["main"] = "config/edit";
        $this->template->viewAdmin($this->theme_data, $data);
    }

    function edit($confID = '') {
        $data = array();
        $post = $this->input->post();
        $data['conf'] = $this->mconfig->get($confID, 'CONF_CODE');
		$data['tresource'] = $this->mresource->get($this->resCode,'RES_CODE');
        $data['error'] = array('err_code' => 0, 'msg' => '');
        if ($post) {
            if ($confID!=0 && $confID !='') {
                $this->mconfig->update($confID, 'CONF_CODE', $post['conf']);
            }
			else{
				$this->mconfig->insert($post['conf']);
			}
            redirect($this->url_edit . $post['conf']['CONF_CODE']);
        }

        $this->theme_data["main"] = "config/edit";
        $this->template->viewAdmin($this->theme_data, $data);
    }
	
	function delete($confID = ''){
		if($confID !=''){
			$info = $this->mcategory->get($confID, 'CONF_CODE');
			if($info->IS_DEFAULT !=1)
				$this->mconfig->delete($confID, 'CONF_CODE');
		}
	}
	
	function upload($old_name = '') {
		$config['upload_path'] = './'.$this->folder_upload;
		$config['allowed_types'] = 'gif|jpg|png';
		
		//$config['max_size']	= '10000';
		//$config['max_width']  = '1024';
		//$config['max_height']  = '768';
		if(!is_dir($this->folder_upload))
			mkdir($this->folder_upload,0777);
		$this->load->library('upload', $config);
		if ( ! $this->upload->do_upload())
		{
			return false;
		}
		else
		{
			if($old_name && isset($old_name))
			unlink('./'.$old_name);
			return array('upload_data' => $this->upload->data());
		}
		
	}
}
